﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Xpo.DB;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using System.IO;
using DevExpress.Utils.Drawing;
using DevExpress.Utils;
using DevExpress.XtraEditors;

namespace sample1.MillionsDataDemo {
    
    public partial class UserControl1 : UserControl {
        //LogWriter writer = new LogWriter();
        public UserControl1() {
            string conn = "data source=(local);integrated security=SSPI;initial catalog=ServerModeGridProjects";
            //conn = "data source=192.168.1.218;user id=sa;password=qscfty;initial catalog=ebadaplatforlog;";
            //xpServerCollectionSource1.ObjectClassInfo=
            IDataStore store = XpoDefault.GetConnectionProvider(conn, AutoCreateOption.DatabaseAndSchema);
            store = new WaitCursorWrapper(store);
            //store = new DataStoreLogger(store, writer);
            XpoDefault.DataLayer = new SimpleDataLayer(store);
            //int nn=CalcRecordCount(conn);
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e) {
            xpServerCollectionSource1.Reload();
        }

        private void gridControl1_Click(object sender, EventArgs e) {

        }
        public static int CalcRecordCount(string serverConnectionString) {
            try {
                using (Session session = new Session()) {
                    session.AutoCreateOption = AutoCreateOption.None;
                    session.ConnectionString = serverConnectionString;
                    return (int)session.Evaluate<ServerSideGridTest>(new AggregateOperand(null, null, Aggregate.Count, null), null);
                }
            } catch {
                return 0;
            }
        }
    }
   
    [DeferredDeletion(false)]
    public class ServerSideGridTest : XPObject {
        public ServerSideGridTest(Session session) : base(session) { }
        
        public string subject;
        [Indexed]
        public string Subject {
            get { return subject; }
            set { subject = value; }
        }
        [Indexed]
        public string From;
        [Indexed]
        public DateTime Sent;
        [Indexed]
        public Int64 Size;
        [Indexed]
        public bool HasAttachment;
    }
    //internal class XConsole : XtraScrollableControl {
    //    public XConsole() {
    //        this.Appearance.Options.UseBackColor = true;
    //        this.Enabled = true;
    //    }
    //    public new bool Enabled {
    //        get {
    //            return base.Enabled;
    //        }
    //        set {
    //            base.Enabled = value;
    //            if (base.Enabled)
    //                this.Appearance.BackColor = Color.White;
    //            else
    //                this.Appearance.BackColor = Color.Gray;
    //        }
    //    }
    //}
    //public class XtraConsole : XtraUserControl {
    //    private PaintWindow paintWin;
    //    private XConsole xtraScrollableControl1;

    //    public XtraConsole() {
    //        InitializeComponent();
    //        this.paintWin.Console = this;
    //        lines.Add(string.Empty);
    //        this.Appearance.ForeColor = Color.Black;
    //    }

    //    private void InitializeComponent() {
    //        this.xtraScrollableControl1 = new XConsole();
    //        this.paintWin = new PaintWindow();
    //        this.xtraScrollableControl1.SuspendLayout();
    //        this.SuspendLayout();
    //        // 
    //        // xtraScrollableControl1
    //        // 

    //        this.xtraScrollableControl1.Controls.Add(this.paintWin);
    //        this.xtraScrollableControl1.Dock = System.Windows.Forms.DockStyle.Fill;
    //        this.xtraScrollableControl1.Location = new System.Drawing.Point(0, 0);
    //        this.xtraScrollableControl1.Name = "xtraScrollableControl1";
    //        this.xtraScrollableControl1.Size = new System.Drawing.Size(438, 367);
    //        this.xtraScrollableControl1.TabIndex = 0;
    //        // 
    //        // paintWin
    //        // 
    //        this.paintWin.Location = new System.Drawing.Point(3, 3);
    //        this.paintWin.Name = "paintWin";
    //        this.paintWin.Size = new System.Drawing.Size(30, 25);
    //        this.paintWin.TabIndex = 0;
    //        // 
    //        // XtraConsole
    //        // 
    //        this.Controls.Add(this.xtraScrollableControl1);
    //        this.Name = "XtraConsole";
    //        this.Size = new System.Drawing.Size(438, 367);
    //        this.xtraScrollableControl1.ResumeLayout(false);
    //        this.ResumeLayout(false);

    //    }
    //    public new bool Enabled {
    //        get {
    //            return base.Enabled;
    //        }
    //        set {
    //            base.Enabled = value;
    //            xtraScrollableControl1.Enabled = value;
    //            this.Refresh();
    //        }
    //    }
    //    List<string> lines = new List<string>();
    //    int CalcStringWidth(string str) {
    //        GraphicsInfo ginfo = new GraphicsInfo();
    //        ginfo.AddGraphics(null);
    //        SizeF textSize = new AppearanceObject(Appearance, DefaultAppearance).CalcTextSize(ginfo.Graphics, str, int.MaxValue);
    //        ginfo.ReleaseGraphics();
    //        return (int)Math.Ceiling(textSize.Width);
    //    }
    //    protected internal void DoPaint(PaintEventArgs e) {
    //        int startLine = e.ClipRectangle.Top / this.Font.Height;
    //        if (startLine >= lines.Count)
    //            return;
    //        int endLine = e.ClipRectangle.Bottom / this.Font.Height + 1;
    //        using (GraphicsCache cache = new GraphicsCache(e)) {
    //            AppearanceObject ao = new AppearanceObject(Appearance, DefaultAppearance);
    //            for (int i = startLine; i < lines.Count && i <= endLine; ++i) {
    //                ao.DrawString(cache, lines[i], new Rectangle(0, i * ao.Font.Height, paintWin.Width, ao.Font.Height));
    //            }
    //        }
    //    }
    //    string buffer = string.Empty;
    //    void WriteBuffer(object sender, EventArgs e) {
    //        string str = buffer;
    //        if (str == null || str.Length == 0)
    //            return;
    //        buffer = string.Empty;
    //        str = str.Replace("\r\n", "\n");
    //        str = str.Replace("\n\r", "\n");
    //        str = str.Replace('\r', '\n');
    //        bool nonFirst = false;
    //        bool linesAdded = false;
    //        foreach (string line in str.Split('\n')) {
    //            if (nonFirst) {
    //                lines.Add(string.Empty);
    //                linesAdded = true;
    //            } else {
    //                nonFirst = true;
    //            }
    //            int pos = lines.Count - 1;
    //            string newValue = lines[pos] + line;
    //            lines[pos] = newValue;
    //            int width = CalcStringWidth(newValue);
    //            if (width >= paintWin.Width)
    //                paintWin.Width = width + 1;
    //        }
    //        if (linesAdded) {
    //            this.paintWin.Height = lines.Count * this.Font.Height;
    //            if (AutoScrollToEnd && paintWin.Height > this.xtraScrollableControl1.ClientRectangle.Height) {
    //                this.xtraScrollableControl1.AutoScrollPosition = new Point(0, 2 + paintWin.Height - this.xtraScrollableControl1.ClientRectangle.Height);
    //            }
    //        }
    //        this.paintWin.Invalidate();
    //    }
    //    public bool AutoScrollToEnd = true;
    //    class PaintWindow : Control {
    //        public XtraConsole Console;
    //        protected override void OnPaint(PaintEventArgs e) {
    //            if (Console != null)
    //                Console.DoPaint(e);
    //        }
    //    }
    //    public void Write(string chunk) {
    //        this.buffer += chunk;
    //        this.BeginInvoke(new EventHandler(WriteBuffer), this, EventArgs.Empty);
    //    }

    //    internal void ClearText() {
    //        buffer = string.Empty;
    //        lines.Clear();
    //        lines.Add(string.Empty);
    //        paintWin.Width = 0;
    //        paintWin.Height = 0;
    //        this.Refresh();
    //    }
    //}
    //public class LogWriter : TextWriter {
    //    bool enabled = true;
    //    public XtraConsole Console;
    //    public LogWriter() { }
    //    public LogWriter(XtraConsole console)
    //        : this() {
    //        this.Console = console;
    //    }
    //    Encoding enc = new UnicodeEncoding(false, false);
    //    public override Encoding Encoding {
    //        get { return enc; }
    //    }
    //    public override void Write(string value) {
    //        if (Console != null && Enabled) {
    //            Console.Write(value);
    //        }
    //    }
    //    public override void Write(char ch) {
    //        Write(ch.ToString());
    //    }
    //    public bool Enabled {
    //        get { return enabled; }
    //        set {
    //            enabled = value;
    //            if (Console != null)
    //                Console.Enabled = enabled;
    //        }
    //    }
    //}
}
